package com.jackrain.nea.fc.cp.mapper;

import com.jackrain.nea.fc.utils.ObjectToUtil;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
public interface DeptInfoMapper {

    @SelectProvider(type = DeptInfoSql.class, method = "findDeptInfoData")
    List<HashMap> selDeptInfo(@Param("code_name") String code_name,
                              @Param("date_beg") String date_beg,
                              @Param("date_end") String date_end);

    class DeptInfoSql {

        public String findDeptInfoData(Map<String, Object> map) {
            String code_name = (String) map.get("code_name");
            String date_beg = (String) map.get("date_beg");
            String date_end = (String) map.get("date_end");
            return new SQL() {
                {
                    SELECT("a.ecode as dept_no," +
                            "a.ename as dept_name," +
                            "b.ecode as parent_no ");
                    FROM("CP_C_HRORG a " +
                            "left join CP_C_HRORG b on a.CP_C_ORGUP_ID = b.id ");
                    WHERE("a.isactive = 'Y' " +
                            "and a.modifieddate BETWEEN #{date_beg} and #{date_end}");
                    if (!ObjectToUtil.isNullOREmpty(code_name)) {
                        WHERE("a.ECODE = " + code_name + " or a.ENAME =" + code_name);
                    }
                }
            }.toString();
        }
    }
}
